// import com.alibaba.druid.sql.ast.SQLObject;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

// import java.util.Collections;
// import java.util.List;

#include "SQLSmallIntExpr.h"

#include "../../visitor/SQLASTVisitor.h"
#include "../../../utils/doubleEqual.h"
#include "../../../utils/StringUtils.h"

SQLSmallIntExpr::SQLSmallIntExpr()
{
}

SQLSmallIntExpr::SQLSmallIntExpr(double value)
    : SQLNumericLiteralExpr()
{
  this->value = value;
}

SQLSmallIntExpr::SQLSmallIntExpr(string_ptr value)
{
  if (StringUtils::startWith(value, make_string_ptr("--")))
  {
    value = make_string_ptr(value->substr(2));
  }
  this->value = atof(value->c_str());
}

SQLObject_ptr SQLSmallIntExpr::clone()
{
  SQLObject_ptr clone_tmp = std::shared_ptr<SQLSmallIntExpr>(new SQLSmallIntExpr(value));
  std::shared_ptr<SQLSmallIntExpr> x = std::dynamic_pointer_cast<SQLSmallIntExpr>(clone_tmp);
  return x;
}

//@Override
SQLObject_list_ptr SQLSmallIntExpr::getChildren()
{
  return SQLObject_list_ptr();
}

//@Override
double SQLSmallIntExpr::getNumber()
{
  return value;
}

//@Override
double SQLSmallIntExpr::getValue()
{
  return value;
}

void SQLSmallIntExpr::setValue(double value)
{
  this->value = value;
}

void SQLSmallIntExpr::accept0(SQLASTVisitor_ptr visitor)
{
  visitor->visit(SharedObject(SQLSmallIntExpr));

  visitor->endVisit(SharedObject(SQLSmallIntExpr));
}

//@Override
int SQLSmallIntExpr::hashCode()
{
  int prime = 31;
  int result = 1;
  // result = prime * result + ((value == nullptr) ? 0 : value.hashCode());
  result = prime * result + int(value);
  return result;
}

//@Override
bool SQLSmallIntExpr::equals(Object_ptr obj)
{
  if (this == obj.get())
  {
    return true;
  }
  if (obj == nullptr)
  {
    return false;
  }
  if (getClass() != obj->getClass())
  {
    return false;
  }
  std::shared_ptr<SQLSmallIntExpr> other = std::dynamic_pointer_cast<SQLSmallIntExpr>(obj);
  // if (value == nullptr) {
  //     if (other.value != nullptr) {
  //         return BOOL::FALSE;
  //     }
  // } else
  if (!doubleEqual::AreEqual(value, other->value))
  {
    return false;
  }
  return true;
}

//@Override
void SQLSmallIntExpr::setNumber(double number)
{
  // if (number == nullptr) {
  //     this->setValue(nullptr);
  //     return;
  // }

  this->setValue((number));
}
